package cn.jiahua.hospital.controller.adminBack;

import cn.jiahua.hospital.dto.Page;
import cn.jiahua.hospital.dto.Result;
import cn.jiahua.hospital.pojo.User;
import cn.jiahua.hospital.service.UserService;
import cn.jiahua.hospital.utils.ResultUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@Api(tags = "超级管理员后台-用户")
@RequestMapping("/api/back/user/")
public class UserBackController {
    @Autowired
    UserService userService;

    @ApiOperation(value = "未拉黑用户分页", produces = "application/json", notes = "根据用户名和性别模糊查询,当前页码和页容量分页")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userName", value = "用户姓名", required = false, dataType = "string"),
            @ApiImplicitParam(name = "userSex", value = "用户性别 ", required = false, dataType = "int"),
            @ApiImplicitParam(name = "page", value = "当前页码 ", required = true, dataType = "int"),
            @ApiImplicitParam(name = "limit", value = "页大小 ", required = true, dataType = "int")
    })
    @GetMapping("/limit")
    public Result limit(String userName,
                        Integer userSex,
                        Integer page,
                        Integer limit) {
        //总数量

        int count = userService.userSelectCount(userName, userSex);
        System.out.println(count);
        Page<User> page1 = new Page<User>();
        page1.setTotal(count);
        page1.setPageNo(page);
        page1.setPageSize(limit);
        //总页数
        int pageCount = page1.getPages();
        //调用service层获取数据
        List<User> userList = userService.userSelectPage(userName, userSex, (page - 1) * limit, limit);

        Map<String, Object> map = new HashMap<String, Object>();
        map.put("page1", page1);
        map.put("userList", userList);
        return ResultUtils.returnSuccess("查询成功", map);
    }
    @ApiOperation(value = "拉黑用户分页", produces = "application/json", notes = "根据用户名和性别模糊查询,当前页码和页容量分页")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userName", value = "用户姓名", required = false, dataType = "string"),
            @ApiImplicitParam(name = "userSex", value = "用户性别 ", required = false, dataType = "int"),
            @ApiImplicitParam(name = "page", value = "当前页码 ", required = true, dataType = "int"),
            @ApiImplicitParam(name = "limit", value = "页大小 ", required = true, dataType = "int")
    })
    @GetMapping("/blacklist/limit")
    public Result blacklistLimit(String userName,
                        Integer userSex,
                        Integer page,
                        Integer limit) {
        //总数量

        int count = userService.userSelectCount1(userName, userSex);
        System.out.println(count);
        Page<User> page1 = new Page<User>();
        page1.setTotal(count);
        page1.setPageNo(page);
        page1.setPageSize(limit);
        //总页数
        int pageCount = page1.getPages();
        //调用service层获取数据
        List<User> userList = userService.userSelectPage1(userName, userSex, (page - 1) * limit, limit);

        Map<String, Object> map = new HashMap<String, Object>();
        map.put("page1", page1);
        map.put("userList", userList);
        return ResultUtils.returnSuccess("查询成功", map);
    }
    @ApiOperation(value = "查看用户", produces = "application/json", notes = "根据用户ID查看用户详细信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "int")
    })
    @GetMapping("/select")
    public Result select(Integer id) {
        User user = userService.getUserById(id);
        return ResultUtils.returnDataSuccess(user);
    }

    @ApiOperation(value = "拉黑用户", produces = "application/json", notes = "根据用户ID进行拉黑拒绝用户登录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "int"),
            @ApiImplicitParam(name = "blacklist", value = "0：正常，1：拉黑", required = true, dataType = "int")
    })
    @GetMapping("/block")
    public Result block(Integer id, Integer blacklist) {
        User user = userService.getUserById(id);
        user.setBlacklist(blacklist);
        if (userService.qdtxModifyUser(user) > 0) {
            return ResultUtils.returnDataSuccess(user);
        }
        return ResultUtils.returnFail("操作失败");
    }

}
